package com.ptszyxx.popose.module.main.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.RxTextTool;
import com.ptszyxx.popose.databinding.FragmentMineLookTabBinding;
import com.ptszyxx.popose.module.main.mine.adapter.MineLookAdapter;
import com.ptszyxx.popose.module.main.mine.vm.MineLookTabVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.mine.MineLookEntity;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YRecyclerViewUtil;

import java.util.List;

public class MineLookTabFragment extends BaseFragment<FragmentMineLookTabBinding, MineLookTabVM> {
    private int tab;
    private MineLookAdapter adapter;

    public MineLookTabFragment(int tab){
        this.tab = tab;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine_look_tab;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MineLookTabVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MineLookTabVM.class);
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new MineLookAdapter(viewModel);
        YRecyclerViewUtil.init(getContext(), binding.recyclerView, adapter);

        binding.refreshLayout.setOnRefreshListener(o ->{
            viewModel.requestLookList(true, tab);
        });
        binding.refreshLayout.setOnLoadMoreListener(o -> {
            viewModel.requestLookList(false, tab);
        });
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestLookList(true, tab);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, new Observer<List<MineLookEntity>>() {
            @Override
            public void onChanged(List<MineLookEntity> list) {
                setAdapterUI(list);
            }
        });
        viewModel.uc.onVipEvent.observe(this, new Observer<List<MineLookEntity>>() {
            @Override
            public void onChanged(List<MineLookEntity> list) {
                setVipUI(list);
            }
        });
    }

    private void setAdapterUI(List<MineLookEntity> list) {
        binding.refreshLayout.finishRefresh();
        binding.refreshLayout.finishLoadMore();
        if (YCollectionUtil.isEmpty(list)) {
            binding.refreshLayout.finishLoadMoreWithNoMoreData();
            if (viewModel.isRefresh()){
                binding.stateLayout.showEmpty();
            }
            return;
        }
        binding.stateLayout.showContent();
        if (viewModel.isRefresh()){
            adapter.setList(list);
        }else{
            adapter.addData(list);
        }
    }


    private void setVipUI(List<MineLookEntity> list) {
        binding.refreshLayout.setVisibility(View.GONE);
        binding.llNoble.setVisibility(View.VISIBLE);
        if (YCollectionUtil.isEmpty(list)){
            binding.llAvatar.setVisibility(View.GONE);
            binding.tvTip.setVisibility(View.GONE);
        }else{
            binding.llAvatar.setVisibility(View.VISIBLE);
            binding.tvTip.setVisibility(View.VISIBLE);

            if (list.size() == 1){
                YImageUtil.show(binding.avatar1, list.get(0).getGustpic());
                binding.avatar2.setVisibility(View.GONE);
                binding.avatar3.setVisibility(View.GONE);
            }else if (list.size() == 2){
                YImageUtil.show(binding.avatar1, list.get(0).getGustpic());
                YImageUtil.show(binding.avatar2, list.get(1).getGustpic());
                binding.avatar3.setVisibility(View.GONE);
            }else {
                YImageUtil.show(binding.avatar1, list.get(0).getGustpic());
                YImageUtil.show(binding.avatar2, list.get(1).getGustpic());
                YImageUtil.show(binding.avatar3, list.get(3).getGustpic());
            }
        }
        binding.avatar4.setText("+" + list.size());

        RxTextTool.getBuilder("", getActivity())
                .append(getResources().getString(R.string.mine_look_tip0))
                .append(list.size() + "")
                .setForegroundColor(getResources().getColor(com.ysg.R.color.pinkColor))
                .append(getResources().getString(R.string.mine_look_tip1))
                .into(binding.tvTip);


    }

}
